﻿Imports System.Data.OleDb
Imports System.Data
Imports System

Public Class ProfilesEdit
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'adding extra information about new users to UserProfiles table
        If Not IsPostBack Then
            Dim conn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
            Dim sql As String = "SELECT [ID],[FirstName],[LastName],[UserName],[Description],[Picture],[Degree],[University],[Country],[AwardName],[AwardDate],[AwardType],[AwardBy],[Languages],[ScientificMembeship],[OtherIssues],[Website],[Email],[FacebookAccount],[TwitterAccount],[LinkedInAccount],[MobileNo],[TelephoneNo] FROM [UserProfiles] WHERE [UserName]=@f1"
            Dim cmdDB = New OleDbCommand(sql, conn)
            cmdDB.CommandType = CommandType.Text
            'We used RegisterUser.UserName instead of User.Identity.Name, because the new user name is not exist yet
            cmdDB.Parameters.AddWithValue("@f1", User.Identity.Name)
            conn.Open()
            'read data from UserProfile table
            Dim dr As OleDbDataReader = cmdDB.ExecuteReader()
            'This will check if there is any record in the table
            If dr.HasRows Then
                dr.Read()
                'IsDBNull is used whether there is null value in the fields or not
                'If Not IsDBNull(dr("FirstName")) Then 
                tb_userFirstName.Text = dr("FirstName")
                'If Not IsDBNull(dr("LastName")) Then 
                tb_userLastName.Text = dr("LastName")
                'If Not IsDBNull(dr("Description")) Then 
                tb_userDescription.Text = dr("Description")
                'If Not IsDBNull(dr("Degree")) Then 
                tb_degree.Text = dr("Degree")
                'If Not IsDBNull(dr("University")) Then 
                tb_university.Text = dr("University")
                'If Not IsDBNull(dr("Country")) Then 
                tb_country.Text = dr("Country")
                'If Not IsDBNull(dr("AwardName")) Then 
                tb_awardName.Text = dr("AwardName")
                'If Not IsDBNull(dr("AwardDate")) Then 
                tb_awardDate.Text = dr("AwardDate")
                'If Not IsDBNull(dr("AwardType")) Then 
                tb_awardType.Text = dr("AwardType")
                'If Not IsDBNull(dr("AwardBy")) Then 
                tb_awardBy.Text = dr("AwardBy")
                'If Not IsDBNull(dr("Languages")) Then 
                tb_languages.Text = dr("Languages")
                'If Not IsDBNull(dr("ScientificMembeship")) Then 
                tb_scientificMembership.Text = dr("ScientificMembeship")
                'If Not IsDBNull(dr("OtherIssues")) Then 
                tb_otherIssues.Text = dr("OtherIssues")
                'If Not IsDBNull(dr("Website")) Then 
                tb_website.Text = dr("Website")
                'If Not IsDBNull(dr("Email")) Then 
                tb_email.Text = dr("Email")
                'If Not IsDBNull(dr("FacebookAccount")) Then 
                tb_facebookAccount.Text = dr("FacebookAccount")
                'If Not IsDBNull(dr("TwitterAccount")) Then 
                tb_twitterAccount.Text = dr("TwitterAccount")
                'If Not IsDBNull(dr("LinkedInAccount")) Then 
                tb_linkedInAccount.Text = dr("LinkedInAccount")
                'If Not IsDBNull(dr("MobileNo")) Then 
                tb_mobileNo.Text = dr("MobileNo")
                'If Not IsDBNull(dr("TelephoneNo")) Then 
                tb_telephoneNo.Text = dr("TelephoneNo")
            End If
            conn.Close()

        End If
    End Sub

    Protected Sub btn_userProfileSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_userProfileSubmit.Click
        Dim newUserProfilePictureName As String = ""
        Dim sqlString As String

        If fu_userProfilePicture.HasFile Then
            Dim myGUID = Guid.NewGuid()
            'Guid if to generate a random and unique user profile pictures
            newUserProfilePictureName = myGUID.ToString() & ".jpg"
            Dim userProfilePictureLocation = Request.MapPath("Images") & "/" & newUserProfilePictureName
            fu_userProfilePicture.SaveAs(userProfilePictureLocation)
        End If

        Dim oleDbconn As New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)

        If fu_userProfilePicture.HasFile Then
            sqlString = "UPDATE UserProfiles SET FirstName=@f1,LastName=@f2,Description=@f3,Picture=@f4,Degree=@f6,University=@f7,Country=@f8,AwardName=@f9,AwardDate=@f10,AwardType=@f11,AwardBy=@f12,Languages=@f13,ScientificMembeship=@f14,OtherIssues=@f15,Website=@f16,Email=@f17,FacebookAccount=@f18,TwitterAccount=@f19,LinkedInAccount=@f20,MobileNo=@f21,TelephoneNo=@f22 WHERE UserName=@f5"
        Else
            sqlString = "UPDATE UserProfiles SET FirstName=@f1,LastName=@f2,Description=@f3,Degree=@f6,University=@f7,Country=@f8,AwardName=@f9,AwardDate=@f10,AwardType=@f11,AwardBy=@f12,Languages=@f13,ScientificMembeship=@f14,OtherIssues=@f15,Website=@f16,Email=@f17,FacebookAccount=@f18,TwitterAccount=@f19,LinkedInAccount=@f20,MobileNo=@f21,TelephoneNo=@f22 WHERE UserName=@f5"
        End If
        Dim cmdDB = New OleDbCommand(sqlString, oleDbconn)
        cmdDB.CommandType = CommandType.Text
        cmdDB.Parameters.AddWithValue("@f1", tb_userFirstName.Text)
        cmdDB.Parameters.AddWithValue("@f2", tb_userLastName.Text)
        cmdDB.Parameters.AddWithValue("@f3", tb_userDescription.Text)
        If fu_userProfilePicture.HasFile Then cmdDB.Parameters.AddWithValue("@f4", "Images/" & newUserProfilePictureName)
        cmdDB.Parameters.AddWithValue("@f6", tb_degree.Text)
        cmdDB.Parameters.AddWithValue("@f7", tb_university.Text)
        cmdDB.Parameters.AddWithValue("@f8", tb_country.Text)
        cmdDB.Parameters.AddWithValue("@f9", tb_awardName.Text)
        cmdDB.Parameters.AddWithValue("@f10", tb_awardDate.Text)
        cmdDB.Parameters.AddWithValue("@f11", tb_awardType.Text)
        cmdDB.Parameters.AddWithValue("@f12", tb_awardBy.Text)
        cmdDB.Parameters.AddWithValue("@f13", tb_languages.Text)
        cmdDB.Parameters.AddWithValue("@f14", tb_scientificMembership.Text)
        cmdDB.Parameters.AddWithValue("@f15", tb_otherIssues.Text)
        cmdDB.Parameters.AddWithValue("@f16", tb_website.Text)
        cmdDB.Parameters.AddWithValue("@f17", tb_email.Text)
        cmdDB.Parameters.AddWithValue("@f18", tb_facebookAccount.Text)
        cmdDB.Parameters.AddWithValue("@f19", tb_twitterAccount.Text)
        cmdDB.Parameters.AddWithValue("@f20", tb_linkedInAccount.Text)
        cmdDB.Parameters.AddWithValue("@f21", tb_mobileNo.Text)
        cmdDB.Parameters.AddWithValue("@f22", tb_telephoneNo.Text)
        cmdDB.Parameters.AddWithValue("@f5", User.Identity.Name)
        oleDbconn.Open()
        cmdDB.ExecuteNonQuery()
        'Dispose will erase every objects in memory to be cleared before going to another page
        'cmdDB.Dispose()
        oleDbconn.Close()
        'oleDbconn.Dispose()
        Response.Redirect("Profiles.aspx?UserName=" & User.Identity.Name)
    End Sub

    Private Function IsDBNull(ByVal p1 As Object) As Boolean
        Throw New System.NotImplementedException
    End Function

End Class